1 using System;
2 using
System.Collections.Generic;
3 using
System.ComponentModel;
4 using
System.Data;
5 using
System.Drawing;
6 using
System.Linq;
7 using
System.Text;
8 using
System.Windows.Forms;
9 using
System.IO;
10
11 namespace
SteganographyBMP
12 {
13     
public partial class Form1 : Form
14     {
15         
private string inPath1; //duong dan toi file anh can giau
16         
17         
private string inPath2; // duong dan toi file anh can giai nen
18         
19
20         
public Form1()
21         {
22             InitializeComponent();
23         }
24
25         
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
26         {
27             
this.Dispose();
28         }
29
30         
private void FitPicture(Image image,PictureBox frame,GroupBox box)
31         {
32             box.Width = image.Width <
512 ? image.Width : 512;
33             frame.Width = image.Width<
512 ? image.Width : 512;
34             box.Height = image.Height<
512 ? image.Height : 512;
35             frame.Height = image.Height<
512 ? image.Height : 512 ;
36         }
37         
private void openImageToHidingToolStripMenuItem_Click(object sender, EventArgs e)
38         {
39             OpenFileDialog openFileDialog1=
new OpenFileDialog();
40             openFileDialog1.Title =
"Chose an Bitmap Image To Hiding";
41             openFileDialog1.Filter =
"Bitmap Image(*.bmp)|*.bmp";
42             
if (openFileDialog1.ShowDialog() == DialogResult.OK)
43             {
44                 inPath1 = openFileDialog1.FileName;
// lay ra duong dan cua file vua mo
45             }
46             
else
47             {
48                 inPath1 =
"";
49             }
50             Bitmap oldBitmap =
new Bitmap(inPath1);
51             FitPicture(oldBitmap,pictureBox1,groupBox1);
52             pictureBox1.Image = oldBitmap;
53         }
54
55         
private byte[] AddMessLengthToAhead(byte[] message)
56         {
57             
int messLen = message.Length; // do dai cua thong diep
58             
byte[] byteLen = BitConverter.GetBytes(messLen); // dua do dai cua thong diep thanh mang cac byte
59             
// thuc ra do dai cua mang byteLen la 4 (du de bieu dien 1 so nguyen 32bit)
60             
byte[] newMess = new byte[messLen + byteLen.Length]; // thong diep moi sau khi da them do dai vao dau (la mang cac byte)
61             
// dua do dai thong diep ve dau
62             
for (int i = 0; i < byteLen.Length; i++)
63                 newMess[i] = byteLen[i];
64             
for (int i = 0; i < messLen; i++)
65                 newMess[i + byteLen.Length] = message[i];
66             
return newMess;
67         }
68
69         
//Tao 1 file Stego tu dau vao
70         
public void CreateStegoFile(string inPath1, string message, string password, string inPath2)
71         {
72             
// kiem tra dau vao xem da hop le hay chua
73             
if (inPath1 == "")
74                 
throw new Exception("Ban chua chon anh de giau tin");
75             
if (inPath2 == "")
76                 
throw new Exception("Ban chua chon noi de luu anh");
77             
if (message == "")
78                 
throw new Exception("Ban chua nhap thong diep de giau");
79             
if (password == "")
80                 
throw new Exception("Ban chua nhap mat khau");
81             
// Mo file dau vao
82             FileStream inStream =
new FileStream(inPath1, FileMode.Open, FileAccess.Read);
83             
84             
// kiem tra xem co phai anh bitmap 24 bits khong
85             
char b = (char)inStream.ReadByte();
86             
char m = (char)inStream.ReadByte();
87             
if (!(b == 'B' && m == 'M'))
88                 
throw new Exception("Khong phai la anh bitmap");
89             
// kiem tra xem co phai la anh 24 bit hay k
90             inStream.Seek(
28,0); // dua con tro ve vi tri byte thu 28
91             
byte[] temp=new byte[2];
92             inStream.Read(temp,
0,2); // so bit/1pixel duoc luu bang 2 byte
93             Int16 nBit= BitConverter.ToInt16(temp,
0); // chuyen mang byte temp[] ve so nguyen 16 bit
94             
if (nBit != 24)
95                 
throw new Exception("Day khong phai la anh 24 bit");
96             
// Doc 54 byte phan header roi dua vao trong outStream
97             
int offset=54;
98             inStream.Seek(
0, 0);
99             
byte[] header = new byte[offset];
100             inStream.Read(header,
0, offset);
101
102             
//viet 54 byte nay vao trong file stego ( file dau ra)
103             FileStream outStream =
new FileStream(inPath2, FileMode.Create, FileAccess.Write);
104             outStream.Write(header,
0, offset);
105             
106             
// ma hoa thong diep va mat khau thanh 1 thong diep duy nhat
107             UnicodeEncoding unicode =
new UnicodeEncoding();
108             
byte[] newMessageByte = AddMessLengthToAhead(unicode.GetBytes(message));// them 4 byte do dai cua message vao dau cua thong diep
109             
// thuc hien tron
110             newMessageByte = CryptoHelper.Encrypt(newMessageByte, password);
111
112             
// thuc hien giau thong diep nay vao trong anh
113             inStream.Seek(offset,
0); // dua con tro ve noi bat dau cua Data o vi tri thu 54 (offset=54)
114             LSBHelper.Encode(inStream, newMessageByte, outStream);
//thay tung bit cua thong diep vao LSB cua inStream va ghi ra outStream
115
116             inStream.Close();
// dong file anh dau vao
117             outStream.Close();
118         }
119         
private void button1_Click(object sender, EventArgs e)
120         {
121             
// mo hop thoai de chon noi luu file stegano
122             SaveFileDialog saveDialog1 =
new SaveFileDialog();
123             saveDialog1.Title=
"Where do you want to save the file?";
124             saveDialog1.Filter =
"Bitmap (*.bmp)|*.bmp";
125
126             
string outPath;
127             
if (saveDialog1.ShowDialog() == DialogResult.OK)
128             {
129                 outPath = saveDialog1.FileName;
130             }
131             
else
132             {
133                 outPath =
"";
134             }
135             
// tao ra 1 file chua thong diep an
136             CreateStegoFile(inPath1, textBox1.Text, textBox2.Text, outPath);
137             
// dua anh nay len pictureBox
138             Bitmap bitmap =
new Bitmap(outPath);
139             FitPicture(bitmap, pictureBox2, groupBox2);
140             pictureBox2.Image = bitmap;
141             saveDialog1.Dispose();
142         }
143
144         
145         
private void openImageToExtractToolStripMenuItem_Click(object sender, EventArgs e)
146         {
147             OpenFileDialog openFileDialog1 =
new OpenFileDialog();
148             openFileDialog1.Title =
"Chose an Image To Extract";
149             openFileDialog1.Filter =
"Bitmap (*.bmp)|*.bmp";
150             
if (openFileDialog1.ShowDialog() == DialogResult.OK)
151             {
152                 inPath2 = openFileDialog1.FileName;
153             }
154             
else { inPath2 = ""; }
155             
// load anh len picture box 3
156             Bitmap bitmap =
new Bitmap(inPath2);
157             FitPicture(bitmap,pictureBox3,groupBox3);
158             pictureBox3.Image = bitmap;
159
160         }
161
162         
private void button3_Click(object sender, EventArgs e) // thuc hien trich thong tin tu Stego file
163         {
164             
// kiem tra xem dau vao co hop le khong? neu khong tao ra cac exception
165             
if (inPath2 == "")
166                 
throw new Exception("Ban chua chon file anh de trich thong tin");
167             
string password = textBox3.Text;
168             
if (password == "")
169                 
throw new Exception("Ban chua nhap mat khau");
170             
// kiem tra xem file dau vao co phai la file Bitmap 24 bit khong?
171             FileStream inStream =
new FileStream(inPath2, FileMode.Open, FileAccess.Read);
172             inStream.Seek(
0, 0); // dua con tro ve dau file
173             
char b = (char)inStream.ReadByte();
174             
char m = (char)inStream.ReadByte();
175             
if (!(b == 'B' && m == 'M'))
176                 
throw new Exception("Day khong phai la file Bitmap");
177             
// kiem tra xem co phai anh bitmap 24 bit khong
178             
int offset = 28;
179             inStream.Seek(offset,
0);
180             
byte[] temp=new byte[2]; // doc vao 2 byte
181             inStream.Read(temp,
0,2); // vi tri 28 va 29
182             
// chuyen temp[] ve so nguyen 16 bit
183             Int16 numOfBit = BitConverter.ToInt16(temp,
0);
184             
if (numOfBit != 24)
185                 
throw new Exception("Khong phai anh 24 bit");
186             
// bat dau tham nhap vao phan data
187             offset =
54;
188             inStream.Seek(offset,
0);
189             
byte[] bLen = new byte[4]; // 4 byte luu tru do dai thong diep
190             bLen = LSBHelper.Decode(inStream,
4); // cho nay khong the dung method FileStream.Read duoc,do 4 byte nay thuc chat la 32 byte trong inStream
191             
//decrypt 4 byte nay de duoc 4 byte thuc su ban dau (do ca 4 byte nay cung duoc ma hoa boi khoa Key[128])
192             bLen = CryptoHelper.Decrypt(bLen, password);
193             
int length = BitConverter.ToInt32(bLen, 0); // chuyen tu mang byte thanh so nguyen
194
195             
// thuc hien doc ra mang thong diep an (van bi Encrypt)
196             inStream.Seek(offset +
4 * 8, 0); // 32 byte dau tien de luu do dai cua thong diep
197             
byte[] buffer=new byte[length]; // su dung mang nay de luu tru tam
198             
try
199             {
200                 buffer = LSBHelper.Decode(inStream, length);
201             }
202             
catch { throw new Exception("Anh nay khong chua thong tin hoac ban da nhap sai mat khau"); } // trong qua trinh trich xuat ra thong diep giau ,neu gap 1 ngoai le nao do,coi nhu trich xuat khong thanh cong ( thong thuong ngoai le phat sinh se la khong du bo nho)
203             
byte[] realHidenMessage = new byte[4 + buffer.Length];
204             realHidenMessage = ConcatTwoByteArray(bLen, buffer);
// them 4 byte vao dau de tien cho viec Decrypt
205             realHidenMessage = CryptoHelper.Decrypt(realHidenMessage, password);
// bay gio ta da duoc mang thong diep thuc su
206             
byte[] hidenMessage = new byte[length]; // bay gio ta chi quan tam den phan thong diep
207             
for (int i = 0; i < length; i++)
208                 hidenMessage[i] = realHidenMessage[i +
4];
209             
// chuyen ve dang string
210             UnicodeEncoding unicode =
new UnicodeEncoding();
211             
string result = unicode.GetString(hidenMessage);
212             textBox4.Text = result;
// hien thi ket qua ra textbox
213
214             inStream.Close();
// dong file dau vao
215         }
216
217         
//noi 2 mang byte lai voi nhau
218         
private byte[] ConcatTwoByteArray(byte[] arr1, byte[] arr2)
219         {
220             
byte[] retArr = new byte[arr1.Length + arr2.Length];
221             
// lay mang thu nhat
222             
for (int i = 0; i < arr1.Length; i++)
223                 retArr[i] = arr1[i];
224             
// noi them mang thu 2
225             
for (int i = 0; i < arr2.Length; i++)
226                 retArr[i + arr1.Length] = arr2[i];
227             
return retArr;
228         }
229     }
230 }



Chương trình mã hóa giấu tin trong ảnh bằng C# 6.553 lượt xem

Gõ tìm kiếm nhanh...